import apis from '@/api/apis'
import Vuex from 'vuex'

export default {
    namespaced: true,
    state: {
        s: 60, //验证码倒计时
        isyan: true,
        tatal: 3, //密码输错次数，
        name: "17760330334",
        userInfo: {}, //用户个人所有信息
        pass: "987654", //记住密码
        noticeData: [] ,//通知信息
		userRisk:{},//风险类型
    },
    mutations: {
        s_jj(state) {
            const a = setInterval(() => {
                state.s--;
                if (state.s == 0) {
                    state.isyan = true
                    clearInterval(a)
                }
            }, 1000)
        },
        //获取验证码开始
        cnm(state) {
            state.s = 60
            state.isyan = false
        },
        total_(state) {
            state.tatal--
        },
        //保存个人信息
        saveUersInfo(state, newdata) {
            state.userInfo = {...newdata }
            console.log(state.userInfo, '状态机个人信息更新')
        },
		//保存余额
		saveM(state, newdata){
			state.userInfo.restAmountAll=newdata
		},
        changepass(state, newdata) {
            state.pass = newdata;
        },changename(state, newdata) {
            state.name = newdata;
        },
        //更新通知信息
        updateNoticeData(state, newdata) {
            state.noticeData = [...newdata]
            state.noticeData.forEach(item => {
                item.content = JSON.parse(item.content)
            })
        },
        //更新风险评测
        updatarisk(state, newdata) {
            state.userRisk = {...newdata }
                // console.log(state.userRisk,'风险跟新')
        },
        //去登录
        goLogin(state) {
			uni.redirectTo({
				url:'/pages/login/login'
			})
            uni.showToast({
                title: '请先登录',
                duration: 2000,
                icon: 'none'
            });
        },
		//去认证
		goReal(state){
			uni.redirectTo({
				url:'/pagesD/t-realAuth/t-realAuth'
			})
			uni.showToast({
			    title: '请先实名认证',
			    duration: 2000,
			    icon: 'none'
			});
		}

    },
    actions: {
        // 获取验证码
        async getVerify(context, newdata) {
            // console.log(newdata, "状态机参数")
            const data = await apis.user.getVerify(newdata)
                // console.log(data, "状态机")
        },
        // 注冊
        async setregister(context, newdata) {

            const data = await apis.user.register(newdata)
                // console.log(data)
            return data
        },
        // 登录
        async Login(context, newdata) {
            const data = await apis.user.accountLogin(newdata)
                // console.log(data, '状态机')
            if (data.code == 200) {
                // context.commit('saveUersInfo', data.data)
            }
            return data
        },
        //根据用户id查询用户信息
        async getUserInfo(context) {

            const data = await apis.user.getUserInfo(uni.getStorageSync('userid'))
                // console.log(data,'状态机获取个人信息data')
            context.commit('saveUersInfo', data)
			context.dispatch('getM')
        },
		//查询余额
		async getM(context) {
		    const data = await apis.user.getM(uni.getStorageSync('userid'))
		        console.log(data,'余额')
		    context.commit('saveM', data.data.restAmount)
		},
        //实名认证
        async realNameAuth(context, newdata) {
            const data = await apis.user.realNameAuth(newdata)
                // console.log(data, '实名认证')
            if (data.code == 200) {
                context.dispatch('getUserInfo')
                uni.switchTab({
                        url: '../../pages/mine/mine'
                    }),
                    uni.showToast({
                        title: '认证成功',
                        duration: 2000,
                    })
            }
			context.dispatch('getUserInfo')
        },
        //验证原密码
        async verifyAassword(context, newdata) {
            const data = await apis.user.verifyAassword({
                    id: uni.getStorageSync('userid'),
                    password: newdata
                })
                // console.log(data,'验证原密码')
            return data.data
        },
        //修改密码
        async passwordChange(context, newdata) {
            const data = await apis.user.passwordChange(newdata)
                // console.log(data, '修改面膜')
            if (data.code == 200) {
                uni.navigateTo({
                        url: '/pages/login/login'
                    }),
                    uni.setStorage({
                        key: 'userid',
                        data: null,
                    }),
                    uni.showToast({
                        title: '修改成功,请重新登录',
                        duration: 2000,
                        icon: 'none'
                    })
            }
        },
        //验证原密码
        async verifyPayPassword(context, newdata) {
            const data = await apis.user.verifyPayPassword({
                    id: uni.getStorageSync('userid'),
                    payPassword: newdata
                })
                // console.log(data,'验证支付密码')
            return data.data
        },
        //修改支付密码
        async payPasswordChange(context, newdata) {
            const data = await apis.user.payPasswordChangeUrl(newdata)
                // console.log(data,'修改支付密码')
            if (data.code == 200) {
                uni.switchTab({
                    url: '/pages/mine/mine'
                })
                uni.showToast({
                    title: '修改成功',
                    duration: 2000,
                    icon: 'none'
                })
            }
        },
        //获取个人中心数据
        async notice(context, newdata) {
            const data = await apis.user.notice(newdata)
                // console.log(data, '通知')
            context.commit('updateNoticeData', data.data)
        },
        //修改通知状态
        async updateNotice(context, newdata) {
            const data = await apis.user.updateNotice(newdata)
                // console.log(data,'修改通知状态')
            context.dispatch('notice', { userId: uni.getStorageSync('userid') })
        },
        //风险评测
        async riskTest(context, newdata) {
            const data = await apis.user.riskTest(newdata)
                // console.log(data,'风险评测结果')
            context.commit('updatarisk', data.data)
        },
		//充值
		async recharge(context,newdata){
			const data = await apis.user.recharge({
				id:uni.getStorageSync('userid'),
				rechargeAmount:parseInt(newdata)
			})
			if(data.msg=='操作成功'){
				context.dispatch('getM')
				uni.navigateTo({
					url:'/pagesC/myAssets/myAssets'
				})
				uni.showToast({
					title: "充值成功",
					duration: 5000
				});
			}else{
				uni.showToast({
					title: data.msg,
					duration: 2000,
					icon:'none'
				});
			}
			console.log(data,'充值')
		}
    }


}